<template>
    <el-form :model="form" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px"
             class="demo-ruleForm login-container">
        <h3 class="title">系统登录</h3>
        <el-form-item prop="username">
            <el-input type="text" v-model="form.username" auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input type="password" v-model="form.password" auto-complete="off" placeholder="密码"></el-input>
        </el-form-item>
        <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
            <el-button type="primary" @click.native.prevent="handleSubmit2" :loading="logining">登录</el-button>
            <el-button type="success" @click.native.prevent="handleSettledIn">店铺入驻</el-button>
            <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
        </el-form-item>
    </el-form>
</template>

<script>
    import {requestLogin} from '../api/api';
    //import NProgress from 'nprogress'
    export default {
        data() {
            return {
                logining: false,
                form: {
                    username: 'itsource',
                    password: '123',
                    userType: 0
                },
                rules2: {
                    username: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                        //{ validator: validaePass }
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        //{ validator: validaePass2 }
                    ]
                },
                checked: true
            };
        },
        methods: {
            handleSettledIn() {
                // 要跳转到路由的路径
                this.$router.push({path: '/shopRegister'});
            },
            handleReset2() {
                this.$refs.ruleForm2.resetFields();
            },
            handleSubmit2(ev) {
                var _this = this;//在执行某一个方法时，他会在方法外，定义一个变量来保存this的对象值
                //当方法内如果出现类似java中内部类时，this关键字指向发生变化
                this.$refs.ruleForm2.validate((valid) => {
                    if (valid) {
                        //_this.$router.replace('/table');
                        this.logining = true;
                        //NProgress.start();
                        //执行登录请求操作，将前端输入的用户名和密码，发送到后台，进行校验
                        _this.$http.post("/login/account",_this.form).then(res=>{
                            console.log(">>>>>>>>>>>>"+JSON.stringify(res.data));
                            let result = res.data;
                            this.logining = false;
                            //1. 将获取的用户信息保存到sessionStorage
                            sessionStorage.setItem('user',JSON.stringify(result.data.loginInfo));
                            sessionStorage.setItem('token',result.data.token);
                            //2. 跳转到首页
                            _this.$router.push({'path':'/'})
                        })

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    }

</script>

<style lang="scss" scoped>
    .login-container {
        /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;

        .title {
            margin: 0px auto 40px auto;
            text-align: center;
            color: #505458;
        }

        .remember {
            margin: 0px 0px 35px 0px;
        }
    }
</style>
